import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);

  // 订单数据
  const orders = [
    {
      id: 'ORDER_001',
      listingId: '1',
      listingTitle: '豪华酒店主楼 - 25层建筑，200间客房',
      buyer: {
        id: 'buyer_1',
        name: '张三',
        avatar: '/images/users/user-1.jpg',
        phone: '138****1234',
        email: 'zhang.san@example.com',
      },
      seller: {
        id: 'seller_1',
        name: '酒店管理集团',
        avatar: '/images/sellers/seller-1.jpg',
        phone: '400-****-0001',
        email: 'contact@hotel-group.com',
      },
      status: 'pending_payment',
      statusName: '待支付',
      price: {
        currency: 'CNY',
        originalPrice: 8_000_000,
        finalPrice: 7_200_000,
        discount: 800_000,
        unit: 'month',
      },
      quantity: 1,
      duration: {
        startDate: '2024-02-01',
        endDate: '2024-08-01',
        months: 6,
      },
      totalAmount: 43_200_000,
      deposit: 7_200_000,
      balance: 36_000_000,
      payment: {
        method: '',
        status: 'pending',
        paidAmount: 0,
        paymentDeadline: '2024-01-25T23:59:59Z',
      },
      requirements: {
        usage: '商务酒店运营',
        specialNeeds: '需要保留现有装修风格',
        contactTime: '工作日 9:00-18:00',
      },
      createdAt: '2024-01-20T10:30:00Z',
      updatedAt: '2024-01-20T10:30:00Z',
      notes: '首次合作，希望能建立长期合作关系',
    },
    {
      id: 'ORDER_002',
      listingId: '2',
      listingTitle: '豪华行政套房1501 - 80平米，城市景观',
      buyer: {
        id: 'buyer_2',
        name: '李四',
        avatar: '/images/users/user-2.jpg',
        phone: '139****5678',
        email: 'li.si@example.com',
      },
      seller: {
        id: 'seller_1',
        name: '酒店管理集团',
        avatar: '/images/sellers/seller-1.jpg',
        phone: '400-****-0001',
        email: 'contact@hotel-group.com',
      },
      status: 'paid',
      statusName: '已支付',
      price: {
        currency: 'CNY',
        originalPrice: 1500,
        finalPrice: 1200,
        discount: 300,
        unit: 'night',
      },
      quantity: 1,
      duration: {
        startDate: '2024-01-25',
        endDate: '2024-01-30',
        nights: 5,
      },
      totalAmount: 6000,
      deposit: 1200,
      balance: 4800,
      payment: {
        method: 'alipay',
        status: 'completed',
        paidAmount: 6000,
        paidAt: '2024-01-20T15:45:00Z',
        transactionId: 'TXN_20240120_001',
      },
      requirements: {
        usage: '商务出差住宿',
        specialNeeds: '需要安静环境，高楼层房间',
        contactTime: '随时',
      },
      createdAt: '2024-01-20T15:30:00Z',
      updatedAt: '2024-01-20T15:45:00Z',
      notes: '商务客户，要求服务质量',
    },
    {
      id: 'ORDER_003',
      listingId: '3',
      listingTitle: '酒店管理系统 - SaaS服务，企业级',
      buyer: {
        id: 'buyer_3',
        name: '王五',
        avatar: '/images/users/user-3.jpg',
        phone: '137****9999',
        email: 'wang.wu@hotel.com',
      },
      seller: {
        id: 'seller_2',
        name: '酒店科技公司',
        avatar: '/images/sellers/seller-2.jpg',
        phone: '021-****-8888',
        email: 'sales@hotel-tech.com',
      },
      status: 'in_progress',
      statusName: '进行中',
      price: {
        currency: 'CNY',
        originalPrice: 50_000,
        finalPrice: 45_000,
        discount: 5000,
        unit: 'month',
      },
      quantity: 1,
      duration: {
        startDate: '2024-01-01',
        endDate: '2024-12-31',
        months: 12,
      },
      totalAmount: 540_000,
      deposit: 90_000,
      balance: 450_000,
      payment: {
        method: 'bank_transfer',
        status: 'partial',
        paidAmount: 90_000,
        paidAt: '2023-12-25T10:00:00Z',
        transactionId: 'TXN_20231225_002',
      },
      requirements: {
        usage: '酒店集团PMS系统',
        specialNeeds: '需要定制化开发，集成现有系统',
        contactTime: '工作日 9:00-17:00',
      },
      createdAt: '2023-12-20T14:20:00Z',
      updatedAt: '2024-01-20T09:15:00Z',
      notes: '需要技术支持和培训',
    },
    {
      id: 'ORDER_004',
      listingId: '4',
      listingTitle: '专业酒店管理团队 - 15人，8年经验',
      buyer: {
        id: 'buyer_4',
        name: '赵六',
        avatar: '/images/users/user-4.jpg',
        phone: '136****7777',
        email: 'zhao.liu@resort.com',
      },
      seller: {
        id: 'seller_3',
        name: '酒店管理咨询公司',
        avatar: '/images/sellers/seller-3.jpg',
        phone: '020-****-6666',
        email: 'service@hotel-consulting.com',
      },
      status: 'completed',
      statusName: '已完成',
      price: {
        currency: 'CNY',
        originalPrice: 150_000,
        finalPrice: 135_000,
        discount: 15_000,
        unit: 'month',
      },
      quantity: 1,
      duration: {
        startDate: '2023-08-01',
        endDate: '2023-12-31',
        months: 5,
      },
      totalAmount: 675_000,
      deposit: 135_000,
      balance: 540_000,
      payment: {
        method: 'bank_transfer',
        status: 'completed',
        paidAmount: 675_000,
        paidAt: '2023-12-31T16:00:00Z',
        transactionId: 'TXN_20231231_003',
      },
      requirements: {
        usage: '度假村管理团队',
        specialNeeds: '需要温泉度假村管理经验',
        contactTime: '工作日 8:00-18:00',
      },
      createdAt: '2023-07-25T11:30:00Z',
      updatedAt: '2023-12-31T16:00:00Z',
      notes: '合作愉快，效果超出预期',
      rating: {
        score: 5,
        comment: '团队专业，服务质量优秀，强烈推荐！',
        ratedAt: '2024-01-05T10:00:00Z',
      },
    },
    {
      id: 'ORDER_005',
      listingId: '5',
      listingTitle: '中餐厅 - 300平米，传统中式美食',
      buyer: {
        id: 'buyer_5',
        name: '孙七',
        avatar: '/images/users/user-5.jpg',
        phone: '135****3333',
        email: 'sun.qi@company.com',
      },
      seller: {
        id: 'seller_4',
        name: '餐饮管理公司',
        avatar: '/images/sellers/seller-4.jpg',
        phone: '0755-****-5555',
        email: 'info@restaurant-mgmt.com',
      },
      status: 'cancelled',
      statusName: '已取消',
      price: {
        currency: 'CNY',
        originalPrice: 50_000,
        finalPrice: 45_000,
        discount: 5000,
        unit: 'month',
      },
      quantity: 1,
      duration: {
        startDate: '2024-03-01',
        endDate: '2024-12-31',
        months: 10,
      },
      totalAmount: 450_000,
      deposit: 45_000,
      balance: 405_000,
      payment: {
        method: '',
        status: 'cancelled',
        paidAmount: 0,
        refundAmount: 0,
      },
      requirements: {
        usage: '企业食堂承包',
        specialNeeds: '需要提供员工餐服务',
        contactTime: '工作日 9:00-17:00',
      },
      cancelReason: '公司搬迁，不再需要此服务',
      cancelledAt: '2024-01-18T14:30:00Z',
      cancelledBy: 'buyer',
      createdAt: '2024-01-15T16:20:00Z',
      updatedAt: '2024-01-18T14:30:00Z',
      notes: '因公司搬迁取消订单',
    },
  ];

  // 过滤逻辑
  let filteredOrders = orders;

  if (query.status) {
    filteredOrders = filteredOrders.filter(
      (order) => order.status === query.status,
    );
  }

  if (query.buyerId) {
    filteredOrders = filteredOrders.filter(
      (order) => order.buyer.id === query.buyerId,
    );
  }

  if (query.sellerId) {
    filteredOrders = filteredOrders.filter(
      (order) => order.seller.id === query.sellerId,
    );
  }

  if (query.keyword) {
    const keyword = query.keyword as string;
    filteredOrders = filteredOrders.filter(
      (order) =>
        order.id.includes(keyword) ||
        order.listingTitle.includes(keyword) ||
        order.buyer.name.includes(keyword) ||
        order.seller.name.includes(keyword),
    );
  }

  if (query.startDate) {
    const startDate = new Date(query.startDate as string);
    filteredOrders = filteredOrders.filter(
      (order) => new Date(order.createdAt) >= startDate,
    );
  }

  if (query.endDate) {
    const endDate = new Date(query.endDate as string);
    filteredOrders = filteredOrders.filter(
      (order) => new Date(order.createdAt) <= endDate,
    );
  }

  // 排序
  const sortBy = (query.sortBy as string) || 'createdAt';
  const sortOrder = (query.sortOrder as string) || 'desc';

  filteredOrders.sort((a, b) => {
    let aValue, bValue;

    switch (sortBy) {
      case 'totalAmount': {
        aValue = a.totalAmount;
        bValue = b.totalAmount;
        break;
      }
      case 'createdAt':
      default: {
        aValue = new Date(a.createdAt).getTime();
        bValue = new Date(b.createdAt).getTime();
        break;
      }
    }

    return sortOrder === 'asc' ? aValue - bValue : bValue - aValue;
  });

  // 分页
  const page = Number.parseInt(query.page as string) || 1;
  const pageSize = Number.parseInt(query.pageSize as string) || 10;
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  const items = filteredOrders.slice(start, end);

  return {
    code: 0,
    data: {
      items,
      total: filteredOrders.length,
      page,
      pageSize,
    },
    msg: '获取订单列表成功',
  };
});
